function HandlePageBackendCategory(gui, operater, now, options) {
  this._gui = gui;
  this._operator = operater;
  this._now = now;
  this._options = options;
    
  this._createElements();
};

HandlePageBackendCategory.prototype._createElements = function() {
  this._loadData();
};

HandlePageBackendCategory.prototype._loadData = function() {
  this._category = null;
  this._retrieveCategory();
};

HandlePageBackendCategory.prototype._verifyData = function() {
  if (this._category) {
    this._oldCategory = MiscUtils.clone(this._category);
    this._updateElements();
  }
};

HandlePageBackendCategory.prototype._retrieveCategory = function() {
  var _self = this;
  if (this._options.id) {
    new RequestUtils()._read('pz_category', null, 'd.oid = ' + this._options.id, null, null, null, null, function(result, params) { _self._category = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                                    _self._verifyData.call(_self);
                                                                                                                                  }, null);
  } else {
    this._category = pz_category.instance();
    this._verifyData();
  }
};

HandlePageBackendCategory.prototype._updateElements = function() {
  var _self = this;

  this._gui.category.value = this._category.name;
  this._gui.category.onchange = function() { _self._category.name = this.value; };
  
  this._gui.save.onclick = function() { if (_self._category.name == '') { 
                                          window.alert('名称不能为空'); 
                                          return;
                                        } 
                                        if (_self._options.id) {
                                          if (_self._oldCategory.name != _self._category.name) {
                                            _self._loadDatas();
                                          } else {
                                            history.back();
                                          }
                                        } else {
                                          new RequestUtils()._write('pz_category', [_self._category], [], function() { history.back(); }, {pos: DOMUtils.findPos(this)}); };
                                        }
  this._gui.cancel.onclick = function() { history.back(); };
};


HandlePageBackendCategory.prototype._loadDatas = function() {
  var _self = this;
  this._expenses = null;
  
  var r = window.confirm("IT WILL TAKE SOME TIME TO AFFECT ALL EXPENSES, CONTINUE?");
  if (r) {
    this._retrieveExpenses();
  }
};

HandlePageBackendCategory.prototype._verifyExpenseData = function() {
  if (this._expenses) {
    this._updateExpenses();
  }
};

HandlePageBackendCategory.prototype._retrieveExpenses = function() {
  var _self = this;
  new RequestUtils()._read('pz_expense', null, 'd.category = \'' + this._oldCategory.name + '\'', null, null, null, null, function(result, params) { _self._expenses = result.data;
                                                                                                                                                     _self._verifyExpenseData.call(_self);
                                                                                                                                                   }, {pos: DOMUtils.findPos(this._gui.save)});
};

HandlePageBackendCategory.prototype._updateExpenses = function() {
  var _self = this;
  var expenses = [];
  for (var i = 0, il = this._expenses.length; i < il; i++) {
    var exp = this._expenses[i];
    exp.category = this._category.name;
    expenses[expenses.length] = exp;
  }
  new RequestUtils()._write2([{n: 'pz_expense', a: expenses, r: []}, {n: 'pz_category', a: [this._category], r: []}], function() { history.back(); }, {pos: DOMUtils.findPos(this._gui.save)});
};

